/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.model.Task;

/**
 * This class is used as a model to show tasks
 * It shows the most relevant properties of the task as the name, the type, if it
 * must be donde in the same shift and the description
 * It allows to add and remove tasks from the model, updating the view of the table
 * @see Task
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMTask extends DefaultTableModel {

    private List<Task> tasks;

    /**
     * Class builder
     * @param tasks The task list to show in the table
     */
    public TMTask(List<Task> tasks) {
        super();

        columnIdentifiers.add("Name");
        columnIdentifiers.add("Type");
        columnIdentifiers.add("Same shift");
        columnIdentifiers.add("Description");
        this.setTasks(tasks);
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public Task getTask(int index) {
        return tasks.get(index);
    }

    public void setTask(int index, Task task) {
        tasks.set(index, task);
        refresh();
    }

    public void addTask(Task task) {
        tasks.add(task);
        refresh();
    }

    public Task getDistrubution(int index) {
        return tasks.get(index);
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
        refresh();
    }

    public void removeTask(int index) {
        tasks.remove(index);
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        for (Task t : tasks) {
            v = new Vector();
            v.add(t.getName());
            v.add(t.getDistribution().getType().toString());
            v.add(String.valueOf(t.getSameShift()));
            v.add(t.getDescription());
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
